package bc

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/bc"
	bcReq "github.com/flipped-aurora/gin-vue-admin/server/model/bc/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"time"
)

type BcTempService struct {
}

// CreateBcTemp 创建bcTemp表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcTempService *BcTempService) CreateBcTemp(bcTemp *bc.BcTemp) (err error) {
	err = global.GVA_DB.Create(bcTemp).Error
	return err
}

// DeleteBcTemp 删除bcTemp表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcTempService *BcTempService) DeleteBcTemp(bcTemp bc.BcTemp) (err error) {
	err = global.GVA_DB.Delete(&bcTemp).Error
	return err
}

// DeleteBcTempByIds 批量删除bcTemp表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcTempService *BcTempService) DeleteBcTempByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]bc.BcTemp{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateBcTemp 更新bcTemp表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcTempService *BcTempService) UpdateBcTemp(bcTemp bc.BcTemp) (err error) {
	err = global.GVA_DB.Save(&bcTemp).Error
	return err
}

// GetBcTemp 根据id获取bcTemp表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcTempService *BcTempService) GetBcTemp(id uint) (bcTemp bc.BcTemp, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&bcTemp).Error
	return
}

func (bcTempService *BcTempService) GetBcTempLits(userName string, now time.Time) (list []bc.BcTemp, err error) {
	var bcTemps []bc.BcTemp
	err = global.GVA_DB.Where("sys_user_name = ? and created_at > ? ", userName, now).Find(&bcTemps).Error
	return bcTemps, err
}

func (bcTempService *BcTempService) DeleteBcTempByPhone(phone string, userName string) (err error) {
	err = global.GVA_DB.Delete(&[]bc.BcTemp{}, "phone = ? and sys_user_name =? ", phone, userName).Error
	return err
}

// GetBcTempInfoList 分页获取bcTemp表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcTempService *BcTempService) GetBcTempInfoList(info bcReq.BcTempSearch) (list []bc.BcTemp, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&bc.BcTemp{})
	var bcTemps []bc.BcTemp
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&bcTemps).Error
	return bcTemps, total, err
}

func (bcTempService *BcTempService) GetBcTempExpireList() (list []bc.BcTemp, err error) {
	// 创建db
	db := global.GVA_DB.Model(&bc.BcTemp{})
	var bcRecords []bc.BcTemp
	// 如果有条件搜索 下方会自动创建搜索语句
	db = db.Where("created_at < ? ", time.Now().AddDate(0, 0, -1))
	err = db.Find(&bcRecords).Error
	return bcRecords, err
}
